//
//  HDCFBankListVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/4.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFBankListVc.h"
#import "SVProgressHUD.h"

@interface HDCFBankListVc ()

@property (nonatomic, strong) NSArray *bankArray;

@end

@implementation HDCFBankListVc

- (void)setTextField:(UITextField *)textField{
    _textField = textField;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"银行列表"];
    self.view.backgroundColor = ZKcolor(245, 245, 245);
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorColor = ZKcolor(245, 245, 245);
    [self initData];
}

- (void)initData{
    [SVProgressHUD show];
    NSDictionary *paramet = @{
                              KparametTwo:@"/public/investor/find-bank"
                              };
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"获取成功"];
        [SVProgressHUD dismissWithDelay:1.0];
        
        NSDictionary *dict = result[@"data"];
        self.bankArray = [NSArray arrayWithArray:dict[@"data"]];
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:dict[@"data"]];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:array forKey:@"bankList"];
        [user synchronize];
        
        
        [self.tableView reloadData];
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.bankArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellid2";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    NSDictionary *dict = self.bankArray[indexPath.row];
    
//    NSString *imageName = [NSString stringWithFormat:@"icon_%@", dict[@"bankCode"]];
//
//    cell.imageView.image = [UIImage imageNamed:imageName];
    cell.textLabel.text = dict[@"bankName"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = ZKcolor(32, 32, 32);
    NSNumber *single = dict[@"singleTransactionLimit"];
    NSNumber *day = dict[@"dailyTradingLimit"];
    NSString *str = [NSString stringWithFormat:@"单笔限额：%@元，单日限额：%@元", single.stringValue, day.stringValue];
    cell.detailTextLabel.text = str;
    cell.detailTextLabel.textColor = ZKcolor(170, 170, 170);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.bankArray[indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(returnWithBankDict:withTextField:)]) {
        [self.delegate returnWithBankDict:dict withTextField:_textField];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
